// packageB/member/group/ywmGroupsDetailed/ywmGroupsDetailed.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    dataList: [],
    loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initTab();
  },

  initTab() {
    let arr = [
      {
        name: "全部",
        value: 0
      },
      {
        name: "发放",
        value: 1
      },
      {
        name: "使用",
        value: 2
      },
      {
        name: "退回",
        value: -1
      },
      {
        name: "过期",
        value: -2
      }
    ];
    this.data.dataList = [];
    arr.forEach((val, index) => {
      this.data.dataList.push({
        name: val.name,
        param: { detail_type: val.value },
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: []
      });
    });
    this.setData({
      dataList: this.data.dataList
    })
    this.getData();
  },
  changeTab(event) {
    this.setData({
      active: event.detail.index
    });
    if(this.data.dataList[this.data.active].list.length>0) return;
    this.getData();
  },
  getData(){
    // detail_type，1-发放，2-使用，-1 退回 -2 过期
    app._getNetWork({
      url: app.getNetAddresss('plugin.ywm-fight-groups.frontend.controllers.open-card-detail.get-open-card-detail'),
      data: {
        detail_type: this.data.dataList[this.data.active].param.detail_type
      },
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.data.dataList[this.data.active].total_page = response.data.last_page;
          this.data.dataList[this.data.active].page = this.data.dataList[this.data.active].page;
          this.setData({
            ['dataList[' + this.data.active + '].list']: response.data.data
          })
        } else {
          app.tips(response.msg)
        }
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.dataList[this.data.active].isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },
  _getMoreData() {
    this.data.dataList[this.data.active].isLoadMore = false;
    if (this.data.dataList[this.data.active].page >= this.data.dataList[this.data.active].total_page) {
      return;
    } else {
      let _url = 'plugin.ywm-fight-groups.frontend.controllers.open-card-detail.get-open-card-detail';
      let page = this.data.dataList[this.data.active].page + 1
      let urlStr = app.getNetAddresss(_url);
      urlStr = urlStr + '&page=' + page;
      app._getNetWork({
        url: urlStr,
        data: {
          detail_type: this.data.dataList[this.data.active].param.detail_type
        },
        success: (resdata)=> {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data.data;
            this.data.dataList[this.data.active].isLoadMore = true;
            this.data.dataList[this.data.active].page = page;
            this.setData({
              ['dataList[' + this.data.active + '].list']: this.data.dataList[this.data.active].list.concat(myData)
            });
          } else {
            this.data.dataList[this.data.active].isLoadMore = false;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})